var t = require("../../@babel/runtime/helpers/interopRequireDefault")(require("../../@babel/runtime/regenerator")), e = require("../../@babel/runtime/helpers/asyncToGenerator"), a = getApp(), r = require("../../http/searchApi.js");

Page({
    data: {
        titleBarHeight: a.global.systemInfo.statusBarHeight + a.global.systemInfo.titleHeight,
        searchPlaceholder: "MAKE TOY FUN",
        historySearchList: [],
        hotSearchList: [],
        hotIpList: [],
        showResult: !1,
        tabIndex: 0,
        tabList: [ "全部", "抽盒", "直购" ],
        resChouheList: [],
        resZhigouList: []
    },
    onLoad: function(t) {
        t.placeholder && this.setData({
            searchPlaceholder: t.placeholder
        }), this.loadHistorySearch(), this.loadHotData();
    },
    onShow: function() {},
    returnOnTap: function() {
        this.data.showResult ? (this.searchOnClear(), this.selectComponent("#searchView").setInputValue(null)) : a.returnUI();
    },
    loadHistorySearch: function() {
        var t = this;
        wx.getStorage({
            key: "HistorySearch",
            success: function(e) {
                t.setData({
                    historySearchList: e.data
                });
            },
            fail: function(e) {
                t.setData({
                    historySearchList: []
                });
            }
        });
    },
    updateHistorySearch: function(t) {
        var e = this.data.historySearchList.filter(function(e) {
            return e != t;
        });
        e.unshift(t);
        e.length > 10 && (e = e.slice(0, 10)), this.setData({
            historySearchList: e
        }), wx.setStorage({
            key: "HistorySearch",
            data: e
        });
    },
    searchOnConfirm: function(t) {
        var e = t.detail;
        if (e) this.setData({
            showResult: !0,
            searchKey: e,
            resChouheList: [],
            resZhigouList: [],
            loading: !0
        }), this.updateHistorySearch(e), this.loadSearchResult(e, ""), this.loadHotProducts(); else {
            var a = this.data.searchPlaceholder;
            a && (this.selectComponent("#searchView").setInputValue(a), this.searchOnConfirm({
                detail: a
            }));
        }
    },
    searchOnClear: function() {
        this.setData({
            showResult: !1,
            tabIndex: 0,
            resScrollTopY: 0
        });
    },
    clearHistoryOnTap: function() {
        this.setData({
            historySearchList: []
        }), wx.setStorage({
            key: "HistorySearch",
            data: []
        });
    },
    historySearchItemOnTap: function(t) {
        var e = t.currentTarget.dataset.key;
        this.selectComponent("#searchView").setInputValue(e), this.searchOnConfirm({
            detail: e
        });
    },
    hotSearchItemOnTap: function(t) {
        var e = t.currentTarget.dataset.data;
        if (e.activity) wx.navigateTo({
            url: e.path
        }); else {
            var a = e.keyWord;
            this.selectComponent("#searchView").setInputValue(a), this.searchOnConfirm({
                detail: a
            });
        }
    },
    ipOnTap: function(t) {
        var e = t.currentTarget.dataset.data, a = e.ipChineseName || e.ipEnglishName;
        this.selectComponent("#searchView").setInputValue(a), this.searchOnConfirm({
            detail: a
        });
    },
    tabItemOnTap: function(t) {
        var e = t.currentTarget.dataset.index;
        e != this.data.tabIndex && this.setData({
            tabIndex: e,
            resScrollTopY: 0
        });
    },
    loadHotData: function() {
        var a = this;
        return e(t.default.mark(function e() {
            var s;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.prev = 0, t.next = 3, r.hotData();

                  case 3:
                    s = t.sent, a.setData({
                        hotSearchList: s.data.searchWords,
                        hotIpList: s.data.hotIps
                    }), t.next = 10;
                    break;

                  case 7:
                    t.prev = 7, t.t0 = t.catch(0), console.log(t.t0);

                  case 10:
                  case "end":
                    return t.stop();
                }
            }, e, null, [ [ 0, 7 ] ]);
        }))();
    },
    loadSearchResult: function(s, i) {
        var n = this;
        return e(t.default.mark(function e() {
            var o;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.prev = 0, a.showLoading(), t.next = 4, r.searchResult(s, i, a.getUsername());

                  case 4:
                    o = t.sent, 0 === i ? n.setData({
                        resZhigouList: o.data.blindBox || []
                    }) : 1 === i ? n.setData({
                        resChouheList: o.data.brightBoxList || []
                    }) : n.setData({
                        resChouheList: o.data.brightBoxList || [],
                        resZhigouList: o.data.blindBox || []
                    }), t.next = 11;
                    break;

                  case 8:
                    t.prev = 8, t.t0 = t.catch(0), console.log(t.t0);

                  case 11:
                    return t.prev = 11, wx.hideLoading(), n.setData({
                        loading: !1
                    }), t.finish(11);

                  case 15:
                  case "end":
                    return t.stop();
                }
            }, e, null, [ [ 0, 8, 11, 15 ] ]);
        }))();
    },
    loadHotProducts: function() {
        var a = this;
        return e(t.default.mark(function e() {
            var s, i;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.prev = 0, t.next = 3, r.hotProducts();

                  case 3:
                    s = t.sent, (i = a.selectComponent("#waterFallView")) && i.initData(null, s.data), 
                    t.next = 11;
                    break;

                  case 8:
                    t.prev = 8, t.t0 = t.catch(0), console.log(t.t0);

                  case 11:
                  case "end":
                    return t.stop();
                }
            }, e, null, [ [ 0, 8 ] ]);
        }))();
    }
});